﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace HeavenvsDarkness
{
    
    public class MyDialogChoseForce : MyDialog
    {
         public enum STATE
        {
            WAIT,
            CHOOSE,
            COMPLETE
        }
        public const int DARKNESS = 0;
        public const int HEAVEN = 1;
        int iWait;
        int iComplete;
        int iChoose;
        STATE _iState;
        int _iKind;

        public int iKind
        {
            get { return _iKind; }
            set { _iKind = value; }
        }
        public STATE iState
        {
            get { return _iState; }
            set 
            { 
                _iState = value;
                if (iState == STATE.WAIT)
                {
                    iTexture = iWait;
                }
            }
        }
        int iDelayTime;

        public MyDialogChoseForce()
        {

        }

        public MyDialogChoseForce(XmlElement ele)
        {
            Items = new List<VisibleGameEntity>();
            InitComponent(ele);
        }

        
        public void InitComponent(XmlElement ele)
        {
            iState = STATE.WAIT;
            if (ele.GetAttribute("Name") == "Darkness")
            {
                iKind = DARKNESS;
            }
            else
            {
                iKind = HEAVEN;
            }
            iWait = int.Parse(ele.GetAttribute("iWait"));
            iChoose = int.Parse(ele.GetAttribute("iChoose"));
            iComplete = int.Parse(ele.GetAttribute("iComplete"));
            iState = STATE.WAIT;
            iTexture = iWait;

            nTexture = int.Parse(ele.GetAttribute("Count"));
            Texture2D[] Texture = new Texture2D[nTexture];
            for (int i = 0; i < Texture.Count(); i++)
            {
                Texture[i] = GameUtil.game.Content.Load<Texture2D>(ele.GetAttribute("Src") + i.ToString("000"));
            }

            Vector3 temp;
            temp.X = float.Parse(ele.GetAttribute("X"));
            temp.Y = float.Parse(ele.GetAttribute("Y"));
            temp.Z = float.Parse(ele.GetAttribute("Z"));
            Position = temp;
            Scale = float.Parse(ele.GetAttribute("Scale"));
            iDelayTime = int.Parse(ele.GetAttribute("DelayTime"));
            MySprite sprite = new MySprite(ele);
            Items.Add(sprite);

        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }

        public override void Update(GameTime gameTime)
        {
            if (gameTime.TotalGameTime.Milliseconds % iDelayTime == 0)
                CheckIndex(); 

            for (int i = 0; i < Items.Count; i++)
            {              
                Items[i].UpdateProperties();
                if (Items[i].GetType() == typeof(MySprite))
                {
                    MySprite temp = (MySprite)Items[i];
                    temp.Index = iTexture;
                }            
            }
            
            
        }

        public void CheckIndex()
        {
            switch (iState)
            {
                case STATE.WAIT:
                    if (iTexture == iChoose - 1)
                    {
                        iTexture = iWait;
                    }
                    else
                    {
                        iTexture = (iTexture + 1) % nTexture;
                    }
                    break;

                case STATE.CHOOSE:
                    if (iTexture == iComplete - 1)
                    {
                        iTexture = iComplete;
                        iState = STATE.COMPLETE;
                    }
                    else
                    {
                        iTexture = (iTexture + 1) % nTexture;
                    }
                    break;

                case STATE.COMPLETE:
                    if (iTexture == nTexture-1)
                    {
                        iTexture = iComplete;                       
                    }
                    else
                    {
                        iTexture = (iTexture + 1) % nTexture;
                    }
                    break;
            }
        }

        public override bool OnEvent(Global.EVENT events, object sender, object arg)
        {
            switch (events)
            {
              /*  case Global.EVENT.GM_MOUSE_CLICKED:
                    if (isCursorStayed())
                    {
                        if (iState == STATE.WAIT || iState== STATE.COMPLETE)
                        {
                            iState = STATE.CHOOSE;
                        }
                        else
                        {
                            Global.BroastCastEvent(Global.EVENT.GM_PLAYER_CHOSENFORCE, this, null);
                        }
                    }
                    break;
                case Global.EVENT.GM_DIALOGFORCE_CHOSEN:
                    MyDialogChooseForce temp = (MyDialogChooseForce)sender;
                    if (sender != this)
                    {
                        temp.iState = STATE.WAIT;
                    }
                    break;*/
            }
                
            return false;
        }
    }
}
